package zkk.com.mengqu.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.Random;

import zkk.com.mengqu.R;
import zkk.com.mengqu.activity.KitchenActivity;
import zkk.com.mengqu.models.Food;
import zkk.com.mengqu.util.AddAnimation;
import zkk.com.mengqu.util.FinalString;

/**
 * Created by Administrator on 2017/4/22.
 * 住家菜品设配器
 */
public class FamilyFoodAdapter extends BaseAdapter {
    private Context context;
    private AddAnimation callBack;
    private List<Food> list;
    public static Handler parentHandler;
    private Message msg = new Message();;

    public FamilyFoodAdapter(Context context , List<Food> list , AddAnimation callBack){
        this.context = context;
        this.list = list;
        this.callBack = callBack;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < 1){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        switch (getItemViewType(position)){
            case 0:
                if (convertView == null){
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_family_food_1 , null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                }else {
                    holder = (ViewHolder)convertView.getTag();
                }
                holder.name.setText(list.get(position).getName());
                holder.price.setText("" + list.get(position).getPrice());
                holder.residue.setText("" + list.get(position).getRemain());

                holder.add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KitchenActivity.shopCount.setVisibility(View.VISIBLE);
                        int count = Integer.parseInt(holder.shopCount.getText().toString().trim());
                        holder.shopCount.setText(++count+"");
                        if (callBack == null) return;
                        callBack.addAction(v);

                        sendHandler(FinalString.CASE_SHOPPING_CAR_ADD,position);
                    }
                });
                holder.subtract.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int count = Integer.parseInt(holder.shopCount.getText().toString().trim());
                        if (count <= 0){
                            count = 0;
                        }else{
                            count--;
                        }
                        holder.shopCount.setText(count+"");

                        sendHandler(FinalString.CASE_SHOPPING_CAR_SUBTRACT,position);
                    }
                });
                break;
            case 1:
                if (convertView == null){
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_family_food_2 , null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                }else {
                    holder = (ViewHolder)convertView.getTag();
                }

                holder.name.setText(list.get(position).getName());
                holder.price.setText("" + list.get(position).getPrice());
                holder.residue.setText("" + list.get(position).getRemain());
                pictureId = new Random().nextInt(14)+1;
                System.out.println("+++++++++++22222222 :::::::"+pictureId);
                holder.dishPicture.setImageResource(imageViewss[pictureId]);
                holder.add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KitchenActivity.shopCount.setVisibility(View.VISIBLE);
                        int count = Integer.parseInt(holder.shopCount.getText().toString().trim());
                        holder.shopCount.setText(++count+"");
                        if (callBack == null) return;
                        callBack.addAction(v);

                        sendHandler(FinalString.CASE_SHOPPING_CAR_ADD,position);
                    }
                });
                holder.subtract.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int count = Integer.parseInt(holder.shopCount.getText().toString().trim());
                        if (count <= 0){
                            count = 0;
                        }else{
                            count--;
                        }
                        holder.shopCount.setText(count+"");
                        sendHandler(FinalString.CASE_SHOPPING_CAR_SUBTRACT,position);
                    }
                });
                break;
        }
        return convertView;
    }
    int[] imageViewss = new int[]{R.drawable.dish15,R.drawable.dish1,R.drawable.dish2,R.drawable.dish3,R.drawable.dish4,R.drawable.dish5,R.drawable.dish6,
            R.drawable.dish7,R.drawable.dish8,R.drawable.dish9,R.drawable.dish10,R.drawable.dish11,R.drawable.dish12,R.drawable.dish13,R.drawable.dish14,};
    int pictureId = 1 ;
    public void sendHandler(int type,int position){
        switch (type){
            case FinalString.CASE_SHOPPING_CAR_ADD:
                msg.what = FinalString.CASE_SHOPPING_CAR_ADD;
                break;
            case FinalString.CASE_SHOPPING_CAR_SUBTRACT:
                msg.what = FinalString.CASE_SHOPPING_CAR_SUBTRACT;
                break;
        }
        Bundle bundle = new Bundle();
        bundle.putString("food" , list.get(position).toString());
        msg.setData(bundle);
        parentHandler.handleMessage(msg);
    }

    class ViewHolder{
        private TextView name;//名字
        private TextView price;//价格
        private TextView residue;//剩余数量
        private TextView shopCount;//购买数量
        private ImageView add;//添加
        private ImageView subtract;//减少
        private ImageView dishPicture;//减少
        public ViewHolder(View view){
            name = (TextView) view.findViewById(R.id.tx_food_name);
            price = (TextView) view.findViewById(R.id.tx_food_price);
            residue = (TextView) view.findViewById(R.id.tx_food_residue);
            shopCount = (TextView) view.findViewById(R.id.shop_count);
            add = (ImageView) view.findViewById(R.id.iv_add);
            subtract = (ImageView) view.findViewById(R.id.iv_subtract);
            dishPicture = (ImageView) view.findViewById(R.id.iv_food_image);
        }
    }
}
